<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* let arr = [
      {id: 1, name: '部门1', pid: 0},
      {id: 2, name: '部门2', pid: 1},
      {id: 3, name: '部门3', pid: 1},
      {id: 4, name: '部门4', pid: 3},
      {id: 5, name: '部门5', pid: 4},
    ]

    function buildTree(data, parentId) {
      let tree = [];
      for (let item of data) {
        if (item.pid === parentId) {
          let children = buildTree(data, item.id);
          const newItem = {...item, children}
          tree.push(newItem);
        }
      }
      return tree;
    }
    console.log('11',buildTree(arr,0))
 */
    /* const arr = [12,34,55]
    const sum = (arr, i) => {
      if(i >= arr.length){
        return 0
      }
      return arr[i] + sum(arr,i+1)
    }
    console.log('sum',sum(arr,0)) */
    //console.log('buildTree',buildTree(arr, 0))
    const arr = [12,34,[4,5,6]]
    /* function flat(arr) {
      let result  = []
      for(const item of arr) {
        if(Array.isArray(item)){
          result = [...result, ...flat(item)]
        } else {
          result = [...result, item]
        }
      }
      return result
    }
 */
    const flat = (arr) => {
      return arr.reduce((acc,cur,curIndex,originArr) => {
        return acc.concat(Array.isArray(cur) ? flat(cur) : [cur])
      },[])
    }
    console.log('003',flat(arr))
  </script>


</body>
</html>